<?php
// +----------------------------------------------------------------------
// | Powered By ThinkPHP 5.0.12
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.bojuwang.net All rights reserved.
// +----------------------------------------------------------------------
// | Date：2018-1
// +----------------------------------------------------------------------
// | Author: ZhengLuwei <512711380@qq.com>
// +----------------------------------------------------------------------

namespace app\bjwlmanager\controller;
use think\Db;

/**
 * 管理员登录类
 */
class Login extends Common
{
    public function index()
    {
        return view();
    }

    /**
     * 管理员登录
     * @param string $userName 登录账号
     * @param string $userPassword 登录密码
     * @param string $userCode 验证码
     */
    public function login()
    {
        $userName=input('post.username');
        $userPassword=input('post.password');
        $userCode=input('post.code');
        if(!isset($userName) || !isset($userPassword) || !isset($userCode))
        {
            json_msg(1,'登录请求参数错误');
        }

        $verify = new \Verify();
        if (!$userCode) {
            json_msg(1,'请输入右侧图中验证码');
        }
        if (!$verify->check($userCode)) {
            json_msg(1,'验证码输入错误');
        }
        unset($verify);

        $where=array();
        $where['manager_name']=$userName;
        $where['password']=md5($userPassword);
        $where['status']=0;
        $userInfo=Db::name('manager')->where($where)->find();
        if($userInfo){
            
            //登陆次数加1
            Db::name('manager')->where('id',$userInfo['id'])->setInc('login_count',1);
            
            //获取更新上次登录时间和IP
            $log=Db::name('manager_log')->where('manager_id',$userInfo['id'])->order('add_time desc')->find();
            $update=array();
            if($log){
                $update['last_ip']=$log['add_ip'];
                $update['last_time']=$log['add_time'];
            }else{
                $update['last_ip']=$_SERVER['REMOTE_ADDR'];
                $update['last_time']=date('Y-m-d H:i:s',time());
            }
            Db::name('manager')->where('id',$userInfo['id'])->update($update);

            //添加登录日志
            $this->addLog($userInfo['id'],$userInfo['manager_name'],'login','登录成功');

            //session管理员信息
            session('managerId',$userInfo['id']);
            session('managerName',$userInfo['manager_name']);

            unset($userInfo);
            json_msg(0,'登陆成功，正在进入系统');
        }else{
            json_msg(1,'账号密码错误或您的账号已被禁止登陆');
        }
    }

    /**
     * 按照指定规则生成验证码
     */
    public function checkVerify()
    {
        $verify = new \Verify();
        $verify->imageH = 32;
        $verify->imageW = 100;
        $verify->codeSet = '0123456789';
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 14;
        return $verify->entry();
    }

    /**
     * 管理员注销登录，清空session
     */
    public function logOut()
    {
        session('managerName',null);
        session('managerId',null);
        $this->redirect('Login/index');
    }

}